load("@rules_rust//rust:defs.bzl", "rust_binary")
load("//rs/tests:common.bzl", "CANISTER_HTTP_RUNTIME_DEPS", "COUNTER_CANISTER_RUNTIME_DEPS", "GRAFANA_RUNTIME_DEPS", "MESSAGE_CANISTER_ENV", "MESSAGE_CANISTER_RUNTIME_DEPS", "STATESYNC_TEST_CANISTER_RUNTIME_DEPS", "UNIVERSAL_CANISTER_ENV", "UNIVERSAL_CANISTER_RUNTIME_DEPS", "UNIVERSAL_VM_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "system_test", "system_test_nns")

package(default_visibility = ["//rs:system-tests-pkg"])

PROXY_CANISTER_RUNTIME_DEPS = [
    # Keep sorted.
    "//rs/rust_canisters/proxy_canister:proxy_canister",
]

CANISTER_HTTP_BASE_DEPS = [
    # Keep sorted.
    "//rs/rust_canisters/dfn_candid",
    "//rs/rust_canisters/proxy_canister:lib",
    "//rs/tests/driver:ic-system-test-driver",
    "//rs/tests/networking/canister_http:canister_http",
    "//rs/types/management_canister_types",
    "@crate_index//:anyhow",
    "@crate_index//:candid",
    "@crate_index//:ic-cdk",
    "@crate_index//:slog",
]

COMMON_DEPS = [
    # Keep sorted.
    "//rs/limits",
    "//rs/registry/subnet_type",
    "//rs/tests/driver:ic-system-test-driver",
    "@crate_index//:anyhow",
    "@crate_index//:itertools",
    "@crate_index//:reqwest",
    "@crate_index//:slog",
    "@crate_index//:slog-async",
    "@crate_index//:slog-term",
]

HTTP_ENDPOINTS_DEPS = [
    # Keep sorted.
    "//rs/crypto/tree_hash",
    "//rs/http_endpoints/public",
    "//rs/http_endpoints/test_agent",
    "//rs/tests/consensus/utils",
    "//rs/types/types",
    "//rs/universal_canister/lib",
    "@crate_index//:candid",
    "@crate_index//:ic-utils",
    "@crate_index//:maplit",
    "@crate_index//:serde_cbor",
    "@crate_index//:url",
    "@crate_index//:wat",
]

system_test_nns(
    name = "canister_http_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + ["//rs/rust_canisters/canister_test"],
)

system_test_nns(
    name = "canister_http_stress_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    tags = ["manual"],  # not meant to be run on CI
    test_timeout = "eternal",
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + [
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/types/types",
        "@crate_index//:futures",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "canister_http_soak_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    tags = [
        "dynamic_testnet",
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + [
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/types/types",
        "@crate_index//:futures",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:tokio",
    ],
)

# NOTE: This test is currently non-functional because API boundary nodes running GuestOS on Farm VMs do not support IPv4.
system_test_nns(
    name = "canister_http_socks_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    tags = [
        "dynamic_testnet",
        "long_test",
    ],
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + [
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
    ],
)

system_test_nns(
    name = "canister_http_correctness_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + [
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/test_utilities",
        "//rs/test_utilities/types",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:assert_matches",
        "@crate_index//:ic-agent",
        "@crate_index//:rand",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "canister_http_fault_tolerance_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    tags = [
        "manual",
    ],
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + [
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/types/types",
        "@crate_index//:ic-utils",
        "@crate_index//:reqwest",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "canister_http_non_replicated_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS + ["//rs/rust_canisters/canister_test"],
)

system_test_nns(
    name = "canister_http_time_out_test",
    env = {
        "PROXY_WASM_PATH": "$(rootpath //rs/rust_canisters/proxy_canister:proxy_canister)",
    },
    tags = [
        "long_test",
    ],
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        CANISTER_HTTP_RUNTIME_DEPS + PROXY_CANISTER_RUNTIME_DEPS,
    deps = CANISTER_HTTP_BASE_DEPS,
)

system_test_nns(
    name = "read_state_test",
    env = MESSAGE_CANISTER_ENV | UNIVERSAL_CANISTER_ENV,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/certification",
        "//rs/crypto/utils/threshold_sig_der",
        "//rs/http_endpoints/public",
        "//rs/limits",
        "//rs/registry/routing_table",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/tests/test_canisters/message:lib",
        "//rs/types/base_types",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:assert_matches",
        "@crate_index//:candid",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:leb128",
        "@crate_index//:reqwest",
        "@crate_index//:serde",
        "@crate_index//:serde_cbor",
        "@crate_index//:slog",
        "@crate_index//:time",
        "@crate_index//:tokio",
        "@crate_index//:url",
    ],
)

rust_binary(
    name = "nns_delegation_test",
    testonly = True,
    srcs = ["nns_delegation_test.rs"],
    crate_name = "ic_systest_nns_delegation_test",
    deps = [
        # Keep sorted.
        "//rs/certification",
        "//rs/crypto/tree_hash",
        "//rs/crypto/utils/threshold_sig_der",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
        "@crate_index//:leb128",
        "@crate_index//:reqwest",
        "@crate_index//:serde",
        "@crate_index//:serde_cbor",
        "@crate_index//:slog",
        "@crate_index//:time",
        "@crate_index//:tokio",
        "@crate_index//:wat",
    ],
)

system_test_nns(
    name = "nns_delegation_mainnet_nns_version_test",
    tags = [
        "long_test",
    ],
    test_driver_target = ":nns_delegation_test",
    # The test will is long because we upgrade a subnet and then wait for a delegation to be refreshed
    # which could take more than 15 minutes in total.
    test_timeout = "eternal",
    uses_guestos_img = False,
    uses_guestos_mainnet_nns_img = True,
    uses_guestos_update = True,
)

system_test_nns(
    name = "nns_delegation_branch_nns_version_test",
    tags = [
        "long_test",
    ],
    test_driver_target = ":nns_delegation_test",
    # The nns_delegation_on_app_subnet_test subtest takes ~10 minutes eating up the time budget for other tests.
    test_timeout = "eternal",
    uses_guestos_img = True,
    uses_guestos_mainnet_nns_img = False,
    uses_guestos_update = True,
)

system_test_nns(
    name = "firewall_max_connections_test",
    flaky = True,  # flakiness rate of 5% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "long_test",
    ],
    runtime_deps = UNIVERSAL_VM_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "@crate_index//:tokio",
        "@crate_index//:tokio-util",
    ],
)

system_test_nns(
    name = "firewall_priority_test",
    tags = [
        "long_test",
    ],
    runtime_deps = UNIVERSAL_VM_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "//rs/nns/governance/api",
        "//rs/protobuf",
        "//rs/registry/canister",
        "//rs/registry/keys",
        "//rs/types/types",
        "@crate_index//:candid",
        "@crate_index//:url",
    ],
)

system_test_nns(
    name = "http_endpoints_public_spec_test",
    env = UNIVERSAL_CANISTER_ENV,
    flaky = False,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = COMMON_DEPS + HTTP_ENDPOINTS_DEPS + [
        "@crate_index//:ic-agent",
        "@crate_index//:tokio",
        "@crate_index//:tokio-util",
    ],
)

system_test_nns(
    name = "network_large_test",
    enable_head_nns_variant = False,  # only run this test with the mainnet NNS canisters.
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of 20% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "system_test_large",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "//rs/tests/consensus/utils",
        "//rs/types/types",
        "@crate_index//:tokio",
        "@crate_index//:tokio-util",
    ],
)

system_test_nns(
    name = "network_reliability_test",
    enable_head_nns_variant = False,  # only run this test with the mainnet NNS canisters.
    flaky = True,  # flakiness rate of 3.45% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
        "system_test_large",
    ],
    runtime_deps = COUNTER_CANISTER_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "//rs/types/base_types",
        "@crate_index//:rand",
        "@crate_index//:rand_chacha",
    ],
)

system_test_nns(
    name = "p2p_performance_test",
    tags = [
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + COUNTER_CANISTER_RUNTIME_DEPS + [
        "//rs/tests:jaeger_uvm_config_image",
    ],
    deps = COMMON_DEPS + [
        "@crate_index//:ic-agent",
    ],
)

system_test_nns(
    name = "query_workload_long_test",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = ["performance"],
    colocated_test_driver_vm_resources = {
        "vcpus": 16,
    },
    enable_head_nns_variant = False,
    flaky = True,  # flakiness rate of over 1.1% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + COUNTER_CANISTER_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "//rs/tests/networking/subnet_update_workload",
    ],
)

system_test_nns(
    name = "state_sync_performance",
    enable_head_nns_variant = False,  # only run this test with the mainnet NNS canisters.
    env = UNIVERSAL_CANISTER_ENV | {
        "STATESYNC_TEST_CANISTER_WASM_PATH": "$(rootpath //rs/rust_canisters/statesync_test:statesync-test-canister)",
    },
    tags = [
        "colocate",
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps =
        GRAFANA_RUNTIME_DEPS +
        STATESYNC_TEST_CANISTER_RUNTIME_DEPS +
        UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "//rs/nns/constants",
        "//rs/nns/governance/api",
        "//rs/registry/canister",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/message_routing/rejoin_test_lib",
        "//rs/types/types",
        "@crate_index//:futures",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "update_workload_large_payload",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = ["performance"],
    colocated_test_driver_vm_resources = {
        "vcpus": 16,
    },
    enable_head_nns_variant = False,
    flaky = True,  # flakiness rate of 1.83% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + COUNTER_CANISTER_RUNTIME_DEPS,
    deps = COMMON_DEPS + [
        "//rs/tests/networking/subnet_update_workload",
    ],
)

system_test(
    name = "cloner_canister_workload",
    env = {
        "CLONER_CANISTER_WASM_PATH": "$(rootpath //rs/tests/networking/canisters:cloner_canister)",
    },
    tags = [
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + COUNTER_CANISTER_RUNTIME_DEPS + [
        "//rs/tests/networking/canisters:cloner_canister",
    ],
    deps = [
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/tests/networking/canisters:cloner_canister_types",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)
